<?php
/**
 * Created by PhpStorm.
 * User: jinqiang
 * Date: 2019/02/04
 * Time: 11:48
 */

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Http\Repository\CommonRepository;
use App\Http\Services\System\UserService;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class UserController extends Controller
{
    //获取用户列表
    public function list(Request $request, UserService $userService)
    {
        if(!in_array(auth()->guard('api')->user()->role, ["admin"])){
            //return error("抱歉, 您还不具备该操作权限");
        }
        $this->validate($request,[
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $userService->search($param);
        return result($res);
    }

    //用户修改
    public function set(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $id = $param['id'];
        $data = [];
        if(isset($param['role'])){
            $data['role'] = $param['role'];
        }
        if(isset($param['phone'])){
            $data['phone'] = $param['phone'];
        }
        if(isset($param['status'])){
            $data['status'] = (Int)$param['status'];
        }
        $res = $userService->setUser($id, $data);
        return $res['code'] === 0 ? success("用户设置成功") : error("用户设置失败", $res);
    }
}
